﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace SpriteLib
{
    public class SpriteRotateEffect:ISpriteEffect
    {
        public event EventHandler OnStart;
        public event EventHandler OnEnd;

        bool isLoop;
        int noOfOccurence;
        private int currentOccurrence;
        private TimeSpan totalElapsedTime = new TimeSpan();

        public ISprite Sprite
        {
            set;
            get;
        }

        public bool IsActive
        {
            set;
            get;
        }

        public float StartOrientation
        {
            set;
            get;
        }

        public float EndOrientation
        {
            set;
            get;
        }

        public TimeSpan StartTime
        {
            set;
            get;
        }

        public TimeSpan Duration
        {
            set;
            get;
        }

        private TimeSpan EndTime
        {
            set;
            get;
        }

        public int Occurrence
        {
            set
            {
                if (value > 0)
                {
                    noOfOccurence = value;
                    isLoop = false;
                }
            }
            get { return noOfOccurence; }
        }

        public bool IsLoop
        {
            set
            {
                isLoop = true;
                noOfOccurence = 0;
            }
            get { return isLoop; }
        }


        public SpriteRotateEffect(float startOrientation, float endOrientation, TimeSpan startTime, TimeSpan duration)
            :this(startOrientation, endOrientation, startTime, duration, 0)
        {
            IsLoop = true;
        }

        public SpriteRotateEffect(float startOrientation, float endOrientation, TimeSpan startTime, TimeSpan duration, int occurrence)
        {
            StartOrientation = startOrientation;
            EndOrientation = endOrientation;
            StartTime = startTime;
            Duration = duration;
            EndTime = StartTime + Duration;
            Occurrence = occurrence;
        }

        public void Update(GameTime gameTime)
        {
            totalElapsedTime += gameTime.ElapsedGameTime;
            TimeSpan elapsedSinceStart = totalElapsedTime - StartTime;
            float currentOrientation;
            if (!IsActive && elapsedSinceStart >= TimeSpan.Zero && totalElapsedTime <= EndTime)
            {
                IsActive = true;
                if(OnStart != null)
                    OnStart(this, null);
                
            }

            if (totalElapsedTime >= StartTime && totalElapsedTime <= EndTime)
            {
                float percentageDone = (float)( elapsedSinceStart.TotalMilliseconds / Duration.TotalMilliseconds);
                currentOrientation = (StartOrientation + (EndOrientation - StartOrientation) *
                    percentageDone);

                Sprite.Orientation = currentOrientation;
            }
            else if (totalElapsedTime > EndTime)
            {
                currentOccurrence++;
                if (!isLoop)
                {
                    if (currentOccurrence >= Occurrence)
                    {
                        if (OnEnd != null && IsActive)
                            OnEnd(this, null);
                        IsActive = false;
                    }
                    else
                    {
                        totalElapsedTime = TimeSpan.Zero;
                    }
                }
                else
                {
                    totalElapsedTime = TimeSpan.Zero;
                }
            }


        }
    }
}
